//
//  CHPopView.m
//  CarFile
//
//  Created by 陈浩 on 2017/4/13.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHPopView.h"
@interface CHPopView()

/**
 *  遮盖按钮
 */
@property (nonatomic,strong) UIButton *cover;
@end

@implementation CHPopView


- (UIButton *)cover {
    if (_cover == nil) {
        _cover = [[UIButton alloc] init];
        _cover.backgroundColor = [UIColor blackColor];
        _cover.alpha = 0.5;
        [_cover addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cover;
}


+(instancetype)popView{
    return [[CHPopView alloc] init];
}

-(id)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {

        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        [self setupUI];
    }
    return self;
    
}

-(void)setupUI{
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, UIScreenW *0.8, 40*ScaleY)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"选择统计时间段";
    titleLabel.font = Font(16);
    titleLabel.textColor = [UIColor colorWithHexString:@"333333" alpha:1.0f];
    [self addSubview:titleLabel];
    
    NSArray *titles = @[@"最近一周统计",@"最近一月统计",@"最近一年统计"];
    
    for (int i = 0; i < titles.count;  i++) {
        UIButton *titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        titleBtn.tag = i;
        titleBtn.titleLabel.font = Font(16);
        titleBtn.layer.borderWidth = 1;
        titleBtn.layer.borderColor = [UIColor colorWithHexString:@"ececec" alpha:1.0f].CGColor;
        titleBtn.layer.cornerRadius = 5;
        titleBtn.layer.masksToBounds = YES;
        [titleBtn setTitle:titles[i] forState:UIControlStateNormal];
        [titleBtn setTitleColor:[UIColor colorWithHexString:@"333333" alpha:1.0f] forState:UIControlStateNormal];
        [titleBtn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        // 设置按钮的内容居左显示
        titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        // 设置按钮的内容全部往右10有间隙
        titleBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        titleBtn.frame = CGRectMake(15, 60*ScaleY + i *(40 + 10)*ScaleY, UIScreenW *0.8 - 30, 40*ScaleY);
        [self addSubview:titleBtn];
        
    }
    
    
    
    
}

-(void)show{
    
    // 获得窗口
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    // 添加遮盖按钮
    self.cover.frame = CGRectMake(0, 0,UIScreenW, UIScreenH );
    [window addSubview:self.cover];
//    self.backgroundColor = [UIColor whiteColor];
//    self.size = CGSizeMake(0, 0);
//    self.center = window.center;
//    [window addSubview:self];
//    [UIView animateWithDuration:0.25 animations:^{
//       
        self.size = CGSizeMake( UIScreenW *0.8, 220*ScaleY);
        self.center = window.center;
    [window addSubview:self];
//    }];

    
}




-(void)dismiss{
    

        [self removeFromSuperview];
        // 移除遮盖
        [self.cover removeFromSuperview];
        _cover = nil;

    
}


-(void)titleBtnClick:(UIButton *)btn{
    
    if ([self.delegate respondsToSelector:@selector(didClickBtnWithTitle:andIndex:)]) {
        [self.delegate didClickBtnWithTitle:btn.titleLabel.text andIndex:btn.tag];
    }
    
    
}
@end
